package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.AdminNoticeBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.NoticeMsgQueryData;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.back.NoticeMsgQueryResult;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.NoticeMsgCreateForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.NoticeMsgQueryForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.admin.vo.NoticeMsgUpdateForm;
import com.cloudbroker.bcs.platform.ltb.middle.entity.NoticeMsg;
import com.cloudbroker.bcs.platform.ltb.middle.entityview.NoticeMsgView;
import com.cloudbroker.bcs.platform.ltb.middle.service.NoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by daibd19671 on 2016/9/27.
 */
@Service
public class AdminNoticeServiceImpl implements AdminNoticeBizService{

    @Resource
    private NoticeService noticeService;

    @Override
    public HSBCPSBackVO create_notice_msg(NoticeMsgCreateForm form) {
        NoticeMsg noticeMsg = new NoticeMsg();
        noticeMsg.setCreate_time(new Date().getTime());
        noticeMsg.setNotice_title(form.getNotice_title());
        noticeMsg.setNotice_content(form.getNotice_content());
        noticeMsg.setCompany_id(form.getCompany_id());
        noticeMsg.setSendercomp_id(form.getSendercomp_id());
        noticeService.create_notice(noticeMsg);
        return VOUtil.setSuccessResult(new HSBCPSBackVO());
    }

    @Override
    public NoticeMsgQueryResult query_notice_msg(NoticeMsgQueryForm form) {
        List<NoticeMsgView> list = noticeService.queryNotice(form);
        NoticeMsgQueryResult noticeMsgQueryResult = new NoticeMsgQueryResult();
        List<NoticeMsgQueryData> dataList =  new ArrayList<>(list.size());
        for(NoticeMsgView noticeMsgView : list){
            NoticeMsgQueryData temp = new NoticeMsgQueryData();
            temp.setNotice_id(noticeMsgView.getNotice_id());
            temp.setNotice_content(noticeMsgView.getNotice_content());
            temp.setNotice_title(noticeMsgView.getNotice_title());
            dataList.add(temp);
        }
        String str = JSON.toJSONString(dataList);
        noticeMsgQueryResult.setData_str(str);
        return VOUtil.setSuccessResult(noticeMsgQueryResult);
    }

    @Override
    public HSBCPSBackVO update_notice_msg(NoticeMsgUpdateForm form) {
        NoticeMsg query = new NoticeMsg();
        query.setNotice_id(Long.valueOf(form.getNotice_id()));
        query.setCompany_id(form.getCompany_id());
        query.setSendercomp_id(form.getSendercomp_id());
        NoticeMsg noticeMsg = noticeService.getNoticeMsg(query);
        if(noticeMsg == null){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.NoticeErrorInfo.APP_NOTICE_NOT_EXIST,
                    HSBCPSBackVO.class);
        }
        noticeMsg.setNotice_title(form.getNotice_title());
        noticeMsg.setNotice_content(form.getNotice_content());
        noticeMsg.setUpdate_time(new Date().getTime());
        noticeMsg.setCompany_id(form.getCompany_id());
        noticeMsg.setSendercomp_id(form.getSendercomp_id());
        noticeService.update_notice_msg(noticeMsg);
        return VOUtil.setSuccessResult(new HSBCPSBackVO());
    }
}
